package com.test.platform.service.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;

@Service
public class RepoConfigService {
    private static final Logger logger = LoggerFactory.getLogger(RepoConfigService.class);
    private final ObjectMapper objectMapper = new ObjectMapper();

    public List<Map<String, String>> loadRepoConfigs() {
        try {
            logger.info("开始加载仓库配置文件...");
            ClassPathResource resource = new ClassPathResource("data/repos.json");
            
            // 使用 TypeReference 来处理复杂的泛型类型
            var typeRef = new com.fasterxml.jackson.core.type.TypeReference<Map<String, List<Map<String, String>>>>() {};
            
            Map<String, List<Map<String, String>>> data = objectMapper.readValue(
                resource.getInputStream(), 
                typeRef
            );
            
            List<Map<String, String>> repos = data.get("repositories");
            logger.info("成功加载到 {} 个仓库配置", repos.size());
            repos.forEach(repo -> 
                logger.info("仓库信息: owner={}, name={}", repo.get("owner"), repo.get("name"))
            );
            return repos;
        } catch (Exception e) {
            logger.error("加载仓库配置失败", e);
            throw new RuntimeException("加载仓库配置失败", e);
        }
    }
}